package com.example.tianwu.daydayup;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.DatabaseMetaData;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Tianwu on 2015/11/19.
 */
public class SaveData {
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;

    public static final String chartData = "charData.dat";
    public static final String histroyPhotos = "historyPhothos.dat";


    /** Create a file Uri for saving an image or video */
    public static Uri getOutputMediaFileUri(int type){
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    public static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "DayDayUp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("DayDayUp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    //File data save or read from database.
    private static final String READ_DATA = "";
    public static ArrayList<Photo> readPhotos(Context cont){
        ArrayList<Photo> photos = new ArrayList<>();

        String[] projection = {"*"};

        DataBase dataBase = new DataBase(cont);
        SQLiteDatabase db = dataBase.getReadableDatabase();

        Cursor c = db.query(
                DataBase.TABLE_NAME,                      // The table to query
                projection,                               // The columns to return
                null,                                // The columns for the WHERE clause
                null,                            // The values for the WHERE clause
                null,                                     // don't group the rows
                null,                                     // don't filter by row groups
                null                                 // The sort order
        );
        int count = c.getCount();
        c.moveToFirst();
        Toast.makeText(cont,Integer.toString(count),Toast.LENGTH_LONG).show();
        for(int i=0;i<count;i++){

            String photoId = c.getString(0);
            String id = c.getString(1);
            String age = c.getString(2);
            String gender = c.getString(3);
            String expression = c.getString(4);
            String beauty = c.getString(5);
            String glass = c.getString(6);
            String date = c.getString(7);
            String fileName = c.getString(8);

            FaceData fd = new FaceData();
            fd.id = id;
            fd.age = Integer.valueOf(age);
            fd.gender = gender;
            fd.expression = Integer.valueOf(expression);
            fd.beauty = Integer.valueOf(beauty);
            fd.glass = Boolean.valueOf(glass);

            Photo photo = new Photo(photoId,fileName,fd,date);
            photos.add(photo);

            c.moveToNext();
        }
        return photos;

    }
    public static long savePhotos(Context cont,Photo photo){

        FaceData fd = photo.getFaceData();

        DataBase dataBase = new DataBase(cont);
        SQLiteDatabase db = dataBase.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Photo.PHOTO_ID, photo.getId());
        values.put(FaceData.ID, fd.id);
        values.put(FaceData.AGE, Integer.toString(fd.age));
        values.put(FaceData.GENDER, fd.gender);
        values.put(FaceData.EXPRESSION, Integer.toString(fd.expression));
        values.put(FaceData.BEAUTY, Integer.toString(fd.beauty));
        values.put(FaceData.GLASS, Boolean.toString(fd.glass));
        values.put(Photo.DATE,photo.getDate());
        values.put(Photo.FILE_NAME,photo.getFileName());

        long newRowId = db.insert(DataBase.TABLE_NAME,null, values);
        Toast.makeText(cont,Long.toString(newRowId),Toast.LENGTH_LONG).show();

        return newRowId;
    }
    public static float[] getLineData(Context cont){
        float[] data = new float[11];
        String[] projection = {FaceData.BEAUTY,FaceData.EXPRESSION};

        String orderBy = Photo.DATE + " DESC";

        DataBase dataBase = new DataBase(cont);
        SQLiteDatabase db = dataBase.getReadableDatabase();

        Cursor c = db.query(
                DataBase.TABLE_NAME,                // The table to query
                projection,                         // The columns to return
                null,                               // The columns for the WHERE clause
                null,                               // The values for the WHERE clause
                null,                               // don't group the rows
                null,                               // don't filter by row groups
                orderBy                                // The sort order
        );

        int count = c.getCount();
        c.moveToFirst();
        Toast.makeText(cont,Integer.toString(count),Toast.LENGTH_LONG).show();
        count = count>10?11:count+1;
        data[0]=count;
        for(int i=1;i<count;i++){
            data[i]=Float.valueOf(c.getString(c.getColumnIndex(FaceData.BEAUTY)));
            c.moveToNext();
        }
        return data;
    }
    public static float[] getPaneData(Context cont){
        float[] data = new float[11];
        String[] projection = {FaceData.BEAUTY,FaceData.EXPRESSION};

        String orderBy = Photo.DATE + " DESC";

        DataBase dataBase = new DataBase(cont);
        SQLiteDatabase db = dataBase.getReadableDatabase();

        Cursor c = db.query(
                DataBase.TABLE_NAME,                // The table to query
                projection,                         // The columns to return
                null,                               // The columns for the WHERE clause
                null,                               // The values for the WHERE clause
                null,                               // don't group the rows
                null,                               // don't filter by row groups
                orderBy                                // The sort order
        );

        int count = c.getCount();
        c.moveToFirst();
        Toast.makeText(cont,Integer.toString(count),Toast.LENGTH_LONG).show();
        count = count>10?11:count+1;
        data[0]=count;
        for(int i=1;i<count;i++){
            data[i]=Float.valueOf(c.getString(c.getColumnIndex(FaceData.EXPRESSION)));
            c.moveToNext();
        }
        return data;
    }

}
